package com.bytedance.service.impl;

import com.bytedance.dao.ADAO;
import com.bytedance.dataobject.A;
import com.bytedance.model.Result;
import com.bytedance.service.AService;
import com.bytedance.util.ClassExamine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class AServiceImpl implements AService {

    @Autowired
    private ADAO adao;

    @Override
    public Result<A> add(A a) {
        Result<A> result = new Result<>();
        A getA = adao.getAById(a.getA());
        if (getA != null) {
            result.setResultFailed("该对象A已存在！");
            return result;
        }
        adao.addA(a);
        result.setResultSuccess("添加对象A成功！", a);
        return result;
    }

    @Override
    public Result<List<A>> add(List<A> aList) {
        Result<List<A>> result = new Result<>();
        for (A a : aList) {
            A getA = adao.getAById(a.getA());
            if (getA != null) {
                result.setResultFailed("该对象A已存在！重复对象A：" + a.getA());
                return result;
            }
        }
        for (A a : aList) {
            adao.addA(a);
        }
        //adao.addAList(aList);
        result.setResultSuccess("批量添加对象A成功！", aList);
        return result;
    }

    @Override
    public Result<A> update(A a) throws Exception {
        Result<A> result = new Result<>();
        A getA = adao.getAById(a.getA());
        if (getA == null) {
            result.setResultFailed("该对象A不存在！");
            return result;
        }
        ClassExamine.objectOverlap(a, getA);
        adao.updateA(a);
        result.setResultSuccess("更新对象A成功！", a);
        return result;
    }

    @Override
    public Result<Integer> delete(String id) {
        Result<Integer> result = new Result<>();
        A getA = adao.getAById(id);
        if (getA == null) {
            result.setResultFailed("该对象A不存在！");
            return result;
        }
        int res = adao.deleteAById(id);
        result.setResultSuccess("删除对象A成功！", res);
        return result;
    }

    @Override
    public Result<List<A>> getLike(String id) {
        Result<List<A>> result = new Result<>();
        List<A> aList = adao.getAList(id);
        result.setResultSuccess("查询对象A成功！", aList);
        return result;
    }
}
